var Id= getQueryString("Id")
$(function () {
    $("#jqGrid").Grid({
        url: '../toolsbuyqualitypro/list?Id=' + Id,
        sortname: 'FILL_TIME',
        sortorder: 'DESC',
        colModel: [
            {label: 'id', name: 'id', index: 'ID', key: true, hidden: true},
            {label: '使用部门', name: 'useDept', index: 'USE_DEPT', width: 80},
            {
                label: '填报日期', name: 'fillTime', index: 'FILL_TIME', width: 80, formatter: function (value) {
                    return transDate(value, 'yyyy-MM-dd')
                }
            },
            {label: '数量', name: 'num', index: 'NUM', width: 80},
            {label: '物品名称', name: 'name', index: 'NAME', width: 80},
            {label: '型号规格', name: 'spec', index: 'SPEC', width: 80},
            {label: '供货商', name: 'producor', index: 'PRODUCOR', width: 80},
            {label: '不合格现象描述', name: 'unquailfied', index: 'UNQUAILFIED', width: 80},
            {
                label: '审批状态',
                name: 'approvalStatus',
                index: 'APPROVAL_STATUS',
                hidden: true,
                width: 80,
                formatter: function (value) {
                    return sysEnums.CHECK_FLAG[value]
                }
            },
            {label: '审批人',hidden: true, name: 'approvalName', index: 'APPROVAL_USER', width: 80},
            {
                label: '审批时间',hidden: true, name: 'approvalTime', index: 'APPROVAL_TIME', width: 80, formatter: function (value) {
                    return transDate(value, 'yyyy-MM-dd')
                }
            },
            {label: '最终处置意见',hidden: true, name: 'endDealOpinion', index: 'END_DEAL_OPINION', width: 80},

            {label: '备注信息', name: 'remark', index: 'REMARK', width: 80},
            {
                label: '审批详情', name: 'devCode', index: 'DEV_CODE', width: 80,
                formatter: function (value, col, row) {
                    return "<button class='btn btn-outline btn-info' id='recordsText' onclick='vm.shviewOption(\"" + escape(JSON.stringify(row)) + "\")'>&nbsp;详情</button>";
                }
            },
            /*{label: '修改时间', name: 'updateTime', index: 'UPDATE_TIME', width: 80},
            {label: '修改人', name: 'updateUser', index: 'UPDATE_USER', width: 80},
            {label: '操作人', name: 'createUser', index: 'CREATE_USER', width: 80},
            {label: '操作时间', name: 'createTime', index: 'CREATE_TIME', width: 80},
            {label: '删除标识', name: 'delFlag', index: 'DEL_FLAG', width: 80},*/
        ]
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        toolsBuyQualityPro: {
            updateTime: '',
            updateUser: '',
            createUser: '',
            createTime: '',
            delFlag: '',
            remark: '',
            endDealOpinion: '',
            approvalTime: '',
            approvalUser: '',
            approvalStatus: '',
            unquailfied: '',
            producor: '',
            spec: '',
            name: '',
            num: '',
            fillTime: '',
            useDept: '',
            id: '',
        },
        ruleValidate: {
            name: [
                {required: true, message: '名称不能为空', trigger: 'blur'}
            ]
        },
        q: {
            name: ''
        }
    },
    methods: {
        //审核详情
        shviewOption: function (res) {
            let data = JSON.parse(unescape(res))
            if (!data.approvalName) {
                data.approvalName = "-"
            }
            let dom = "<div class='details' >" +
                "<table>" +
                "<tr><td>审批人：</td><td>" + data.approvalName + "</td></tr>" +
                "<tr><td>审批状态：</td><td>" + sysEnums.CHECK_FLAG[data.approvalStatus] + "</td></tr>" +
                "<tr><td>审批时间：</td><td>" + transDate(data.approvalTime, "yyyy-MM-dd") + "</td></tr>" +
                "<tr><td>最终处置意见：</td><td>" + data.endDealOpinion + "</td></tr>" +
                "</table></div>"
            layer.open({
                type: 1,
                title: "详情",
                closeBtn: 0,
                area: ['450px'],
                offset: '10vh',
                shadeClose: true,
                skin: 'yourclass',
                content: dom
            });
        },
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.toolsBuyQualityPro = {
                updateTime: '',
                updateUser: '',
                createUser: '',
                createTime: '',
                delFlag: '1',
                remark: '',
                endDealOpinion: '',
                approvalTime: '',
                approvalUser: '',
                approvalStatus: '0',
                unquailfied: '',
                producor: '',
                spec: '',
                name: '',
                num: '',
                fillTime: new Date(),
                useDept: '',
                id: '',
            };
        },
        update: function (event) {
            var id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id);
        },
        saveOrUpdate: function (event) {
            var url = vm.toolsBuyQualityPro.id == (null || '') ? "../toolsbuyqualitypro/save" : "../toolsbuyqualitypro/update";
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.toolsBuyQualityPro),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    url: "../toolsbuyqualitypro/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../toolsbuyqualitypro/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.toolsBuyQualityPro = r.toolsBuyQualityPro;
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        reloadSearch: function () {
            vm.q = {
                name: ''
            };
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        /**
         * 审核
         */
        review: function () {
            var result = getSelectedRowData("#jqGrid");
            var id = result.id;
            if (id == null) {
                return;
            }
            if (result.approvalStatus == "已审核") {
                alert('该检测记录已审核,请勿重复操作!', function (index) {
                    vm.reload();
                })
                return;
            }
            vm.getInfo(id)
            openWindow({
                title: "审核",
                area: ['450px', '450px'],
                content: jQuery("#reviewLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    Ajax.request({
                        url: '../toolsbuyqualitypro/review',
                        params: JSON.stringify(vm.toolsBuyQualityPro),
                        type: 'POST',
                        contentType: 'application/json',
                        successCallback: function (result) {
                            setTimeout(function () {
                                location.reload()
                            }, 1000);
                        }
                    })
                }
            })
        },
    }
});